package org.originit.factory;

import org.originit.exception.BeansException;
import org.originit.factory.config.BeanDefinition;
import org.originit.factory.support.AbstractAutowireCapableBeanFactory;
import org.originit.factory.support.BeanDefinitionRegistry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {

    private final Map<String,BeanDefinition> definitionMap = new ConcurrentHashMap<>();

    @Override
    protected BeanDefinition getBeanDefinition(String name) throws BeansException {
        BeanDefinition beanDefinition = definitionMap.get(name);
        if (beanDefinition == null) {
            throw new BeansException("no bean named:" + name + " defined");
        }
        return beanDefinition;
    }

    @Override
    public void registerBeanDefinition(String name, BeanDefinition definition) {
        definitionMap.put(name,definition);
        definition.setName(name);
    }
}
